#include "uuCommandProcess.h"
#include "can.h"
#include "stdio.h"


//模块开关机状态
#define MODULES_TURNON				0x00
#define MODULES_TURNOFF				0x01


/*
 *	扩展帧头:	4 bits			4 bits				7 bits				5 bits				9 bits
 *	Id field:	Protocol	MonitorAddress		ModuleAddress		ProductionDate		SerialNumberLowerPart
 *	content :	  0x01			0x01~0x0F	  	 0x01-0x7F			01-31(optional)			optional
 *							(0x00:broadcast)	(0x00:broadcast)			0					0
 */

/*
 *	数据字段  :				  Byte0					Byte1		Byte2~3		Byte4~7
 *	data field:		GroupAdress		MessageType		CommandType		Reserved	CommandData
 *				   7:4(0x01~0x0F)	3:0()				7:0			7:0 7:0		   
 * 						 
 */

/*
 *@author	hsm
 *@brief	模块开机
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuStart(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion 	= 0x01;
	moduleFrameId.bit.monitorAddress 	= 0x01;
	moduleFrameId.bit.moduleAddress 	= moduleAddr;
	moduleFrameId.bit.productionData 	= 0x00;
	moduleFrameId.bit.serialNumPart 	= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = START_STOP_MODULE;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = MODULES_TURNON;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//模块开机指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}

/*
 *@author	hsm
 *@brief	模块关机
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuShutDown(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};

	UU_ModuleFrameIdU moduleFrameId;
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = START_STOP_MODULE;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = MODULES_TURNOFF;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//模块关机指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	调节模块输出基准电压
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	setVoltage	需要设置的输出电压值	
 *@return	指令发送状态
 */
uint8_t uuSetVout(uint8_t moduleAddr, uint8_t groupAddr,uint32_t setVoltage)
{
	uint8_t sendMsg[8] = {0x00};
	UU_ModuleFrameIdU moduleFrameId;
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = V_OUT_REFERENCE;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = (setVoltage>>24)&0x000000ff;
	sendMsg[5] = (setVoltage>>16)&0x000000ff;
	sendMsg[6] = (setVoltage>>8)&0x000000ff;
	sendMsg[7] = setVoltage&0x000000ff;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//调压指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	调节模块输出电流
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	setVoltage	需要设置的输出电流值	
 *@return	指令发送状态
 */
uint8_t uuSetIout(uint8_t moduleAddr, uint8_t groupAddr, uint32_t setCurrent)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = I_OUT_LIMIT;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = (setCurrent>>24)&0x000000ff;
	sendMsg[5] = (setCurrent>>16)&0x000000ff;
	sendMsg[6] = (setCurrent>>8)&0x000000ff;
	sendMsg[7] = setCurrent&0x000000ff;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//调电流指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读模块输出电压指令
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadVout(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = V_OUT;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读电压指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}

/*
 *@author	hsm
 *@brief	读模块输出电流值
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadIout(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = I_OUT_SLOW;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读电流指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读模块温度
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadTemp(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = INLET_TEMPERATURE;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读模块温度指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
	
}

/*
 *@author	hsm
 *@brief	读模块状态标志位信息
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadStatusFlag(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = STATUS_FLAG;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读模块状态指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读取模块三相电压（AB线电压）
 *@date		2024/07/18
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadVab(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
		uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = V_AB;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读模块状态指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读取模块三相电压（BC线电压）
 *@date		2024/07/18
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadVbc(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
		uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = V_BC;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读模块状态指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读取模块三相电压（CA线电压）
 *@date		2024/07/18
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
 */
uint8_t uuReadVca(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
		uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = V_CA;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读模块状态指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	设置模块组地址
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	setGroupAddr需设置的组地址
 *@return	指令发送状态
 */
uint8_t uuSetGroupAddress(uint8_t moduleAddr, uint8_t groupAddr, uint32_t setGroupAddr)
{
	uint8_t sendMsg[8] = {0x00};
	UU_ModuleFrameIdU moduleFrameId;
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = GROUP_ADDRESS;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = (setGroupAddr>>24)&0x000000ff;
	sendMsg[5] = (setGroupAddr>>16)&0x000000ff;
	sendMsg[6] = (setGroupAddr>>8)&0x000000ff;
	sendMsg[7] = setGroupAddr&0x000000ff;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//设置模块组地址指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	设置模块高/低电压模式
 *@note		It only can be set up successfully when the module is shut down
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	hiLoMode	HIGHVOL_MODE(1): 1000V;	LOWVOL_MODE(2): 500V
 *@return	指令发送状态
*/
uint8_t uuSetHiLoMode(uint8_t moduleAddr, uint8_t groupAddr, uint32_t hiLoMode)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | SETDATA;
	sendMsg[1] = HI_LO_Mode_SELECT;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = hiLoMode;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//设置模块高低压模式指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	读取模块高/低电压模式
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	reserved
 *@return	指令发送状态
*/
uint8_t uuReadHiLoMode(uint8_t moduleAddr, uint8_t groupAddr, uint32_t reserved)
{
	uint8_t sendMsg[8] = {0x00};
	
	UU_ModuleFrameIdU moduleFrameId;	
	moduleFrameId.bit.unregist 			= 0x00;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | READDATA;
	sendMsg[1] = HI_LO_Mode_STATUS;
	sendMsg[2] = 0x00;
	sendMsg[3] = 0x00;
	sendMsg[4] = 0x00;
	sendMsg[5] = 0x00;
	sendMsg[6] = 0x00;
	sendMsg[7] = 0x00;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//读取模块高低压模式指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
/*
 *@author	hsm
 *@brief	AllSetData指令
 *@date		2024/04/30
 *@file		uuCommandProcess.c
 *@param	moduleAddr	模块地址
 *@param	groupAddr	模块组地址
 *@param	IoutLimit	
 *@param	Vbattery	
 *@param	VoutReference
 *@param	unRegistered
 *@return	指令发送状态
*/
uint8_t uuAllSetData(uint32_t moduleAddr, uint8_t groupAddr, uint32_t IoutLimit,uint32_t Vbattery, uint32_t VoutReference)
{
	uint8_t sendMsg[8] = {0x00};
	UU_ModuleFrameIdU moduleFrameId;
	moduleFrameId.bit.protocolVersion	= 0x01;
	moduleFrameId.bit.monitorAddress	= 0x01;
	moduleFrameId.bit.moduleAddress		= moduleAddr;
	moduleFrameId.bit.productionData	= 0x00;
	moduleFrameId.bit.serialNumPart		= 0x00;
	
	sendMsg[0] = (groupAddr<<4) | ALL_SETDATA;
	//Byte1 OnOff_and_HiLoMode 7:6 HiLoModeSelection(NoSelection-0, HiMode-1, LowMode-2) 5:2 	reserved(0) 1:0 OnOff(TurnOn-0, ShurDown(DC)-1, TurnOn(DC,PFC)-2, ShutDown(DC,PFC)-3)
	sendMsg[1] = 0;
	sendMsg[2] = (IoutLimit>>8)&0x000000ff;
	sendMsg[3] = IoutLimit&0x000000ff;
	sendMsg[4] = (Vbattery>>8)&0x000000ff;
	sendMsg[5] = Vbattery&0x000000ff;
	sendMsg[6] = (VoutReference>>8)&0x000000ff;
	sendMsg[7] = VoutReference&0x000000ff;
	
	if(MsgQue_Push(&ModuleSendQue, moduleFrameId.frameId, sendMsg))//设置AllSetData指令入队
	{
		canx_SendData(&hcan1, &ModuleSendQue, UU_MODULECAN_MSGLEN);
		return 1;
	}
	return 0;
}
